/**
 * @Title: 充电报文类，所有请求需要验证第三方ip和用户卡号
 * @Package com.dz.ss.controller
 * @author pengyu
 * @date 2015年12月15日 上午10:21:55
 * @version V1.0
 */
package com.zb.mvc.controller.middleware;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForHardware;
import com.zb.service.middleware.OmsChargeService;
import com.zb.util.StringUtil;

@Controller
public class ChargeFromThirdController {
	
	/**
	 * 日志对象
	 */
	private Logger logger = LoggerFactory.getLogger(ChargeFromThirdController.class);
	@Autowired
	OmsChargeService omsChargeService;
	// 3.1.18. 
	//AS 请求 OMS ，OMS 下发充电 socket，只下发，短连接
	@RequestMapping("/startThirdCharge") 
	public JsonCommon<String> startThirdCharge(HttpServletRequest request, HttpServletResponse response) throws Exception {
//		String ip = request.getRemoteAddr();
//		String userCard = request.getParameter("userCard");
//		//验证ip和 userCard
//		String chargerId = request.getParameter("chargerId");
//		return omsChargeService.startCharge(chargerId, userCard);
		//ev3c报文头
		String chargerId = request.getParameter("chargerId");
		//ev3c报文体
		String portNo = request.getParameter("portNo");
		String controlType = request.getParameter("controlType");
		String openRestrictedData = request.getParameter("openRestrictedData");//开启限制数据：不同充电模式下对应的计算量作为停机条件。自动充满停机方式下无意义。低字节在前，高字节在后。电量控制,单位为“度”，精确到0.01,传输数据放大100倍；时间控制，单位为“秒“，精确到1,；金额控制，单位为“元”，精确到0.01，传输数据放大100倍。
		//定时启动时间，分，时，日，月，对于即时充电，该时间无意义   例如：定时1月14日12时50分开始启动充电，则发送0x01,0x14, 0x12,0x50
		String startTime = request.getParameter("startTime");
		String userCard = request.getParameter("userCard");
		//充电数据上传间隔	BIN码	1		每两次充电数据上传间的间隔时间，单位：秒
		String charging_data_upload_interval = request.getParameter("charging_data_upload_interval");
		//电池详情上传间隔  每两次电池详情上传间的间隔时间，单位：秒
		String battery_details_upload_interval = request.getParameter("battery_details_upload_interval");
		Map<String,String> map  = new HashMap<String,String>();
		map.put("chargerId", chargerId);
		map.put("portNo",portNo);
		map.put("controlType",controlType);
		map.put("openRestrictedData",openRestrictedData);
		map.put("startTime",startTime);
		map.put("userCard",userCard);
		map.put("charging_data_upload_interval",charging_data_upload_interval);
		map.put("battery_details_upload_interval",battery_details_upload_interval);
//		return omsChargeService.startCharge(chargerId, userCard);
		omsChargeService.startCharge(map);

		return null;
	}	
	
	//AS 请求 OMS ，OMS 下发充电 socket，只下发，短连接  startChargeReturn  //php中叫stopCharge
	@RequestMapping("/stopThirdCharge") 
	public JsonCommon<String> stopThirdCharge(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String responseMsgHeader,responseMsgBody,code,msg; 
		String ip = request.getRemoteAddr();
		String userCard = request.getParameter("userCard");
		//验证ip和 userCard
		
		String chargerId = request.getParameter("chargerId");//AS传值
//		String code_type = "12";
		String dec_port = "01";//为什么是01不是00
		String control_type = "04";
		responseMsgBody = dec_port+control_type+userCard;
		responseMsgHeader ="68"+StringUtil.complement(String.valueOf(responseMsgBody.length()), 4)+"01"+"000000"+"0001"+chargerId+"12"+StringUtil.now14time();
		StringUtil.sendSocket(responseMsgHeader+responseMsgBody);
		JsonCommon<String> json = new JsonCommon<String>();
		json.setCode("200");
		json.setMsg(chargerId+" "+userCard+"stopCharge sendSocket success");
		json.setPojo(responseMsgHeader+responseMsgBody);
		return json;		
	}		
		
	
}
